package com.keywaysoft.workflow.controller;


import com.keywaysoft.workflow.dto.StartTaskDTO;
import com.keywaysoft.workflow.service.IProcessEngine;
import com.keywaysoft.workflow.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * @author zhaojx<br>
 * @version 1.0<br>
 * @createDate 2021/11/16 17:34 <br>
 * @Description <p> 启动流程实例 </p>
 */

@RestController
@Api(tags = "流程实例相关")
@Slf4j
@RequestMapping("instance")
public class StartController {
    @Autowired
    IProcessEngine processEngine;

    @PostMapping(path = "start")
    @ApiOperation(value = "根据流程key启动流程", notes = "每一个流程有对应的一个key这个是某一个流程内固定的写在bpmn内的")
    public R start(@Valid @RequestBody StartTaskDTO dto) {
        return processEngine.start(dto);
    }


    @PostMapping(path = "searchByKey")
    @ApiOperation(value = "根据流程key查询流程实例", notes = "查询流程实例")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "processDefinitionKey", value = "流程key", dataType = "String", paramType = "query"),
    })
    public R searchProcessInstance(@RequestParam("processDefinitionKey") String processDefinitionKey) {
        return processEngine.searchProcessInstance(processDefinitionKey);
    }

    /**
     * 挂起、激活流程实例 （挂起、激活单个流程实例）
     */
    @PostMapping(value = "suspend")
    @ApiOperation(value = "根据实例ID挂起/激活流程实例", notes = "根据实例ID挂起/激活流程实例（针对发起的流程进行挂起）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "processInstanceId", value = "实例ID", dataType = "String", paramType = "query"),
    })
    public R updateState(@RequestParam String processInstanceId) {
        return processEngine.updateState(processInstanceId);
    }


    @PostMapping(path = "searchById")
    @ApiOperation(value = "根据流程ID查询流程实例", notes = "查询流程实例")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "processId", value = "流程实例ID", dataType = "String", paramType = "query"),
    })
    public R searchByID(@RequestParam("processId") String processId) {
        return processEngine.searchByID(processId);
    }


    @PostMapping(path = "deleteProcessInstanceByID")
    @ApiOperation(value = "根据流程实例ID删除流程实例", notes = "根据流程实例ID删除流程实例")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "processId", value = "流程实例ID", dataType = "String", paramType = "query"),
    })
    public R deleteProcessInstanceByID(@RequestParam("processId") String processId) {
        return processEngine.deleteProcessInstanceByID(processId);
    }


    @PostMapping(path = "deleteProcessInstanceByKey")
    @ApiOperation(value = "根据流程实例key删除流程实例", notes = "根据流程实例key删除流程实例")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "processDefinitionKey", value = "流程实例Key", dataType = "String", paramType = "query"),
    })
    public R deleteProcessInstanceByKey(@RequestParam("processDefinitionKey") String processDefinitionKey) {
        return processEngine.deleteProcessInstanceByKey(processDefinitionKey);
    }

}